package chapter19;

import java.time.Instant;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/24 21:46
 */
public class RacerWithCountdownLatch {
    public static void main(String[] args) throws InterruptedException {
        final int num = 10;
        CountDownLatch latch = new CountDownLatch(1);
        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            threads[i] = new Thread(new Racer2(latch));
            threads[i].start();
        }

        TimeUnit.SECONDS.sleep(3);
        latch.countDown();
    }

    static class Racer2 implements Runnable {
        private CountDownLatch latch;

        public Racer2(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void run() {
            try {
                latch.await();
                System.out.println(Thread.currentThread().getName() + " start run at" + Instant.now().toEpochMilli());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
